﻿using System;
using System.Linq;
using System.Reflection;
using FluentAssertions;
using LINQPad.OpenAccess.Metadata.Reflection;
using LINQPad.OpenAccess.Metadata.Reflection.Locators;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Telerik.OpenAccess.Metadata;
using Telerik.OpenAccess.Metadata.Fluent;

namespace LINQPad.OpenAccess.Tests.Metadata.Reflection.Locators
{
    /// <summary>
    /// Summary description for FluentMetadataSourceLocatorFixture
    /// </summary>
    [TestClass]
    public class FluentMetadataSourceLocatorFixture
    {
        private readonly FluentMetadataSourceLocator _locator;

        public FluentMetadataSourceLocatorFixture( )
        {
            this._locator = new FluentMetadataSourceLocator( new ReflectionService( ) );
        }

        [TestMethod]
        public void LocatesMetadataSourcesFromTestAssembly( )
        {
            var metadataSources = this._locator.GetFromAssembly( Assembly.GetExecutingAssembly( ) );

            metadataSources.Should( )
                           .NotBeNull( );

            var arrMetadataSources = metadataSources.ToArray( );

            arrMetadataSources.Length.Should( ).Be( 2, "our test assembly contains a designer and a custom fluent metadata source" );

            arrMetadataSources.Should( )
                              .Contain
                                    (
                                        s => s.MappingType == MappingDefinitionType.Fluent
                                                && s.Name == "SofiaCarsRental21ModelFluentMetadataSource"
                                                && s.Path == "LINQPad.OpenAccess.Tests.TestModel.Fluent"
                                                && s.Source is FluentMetadataSource
                                    )
                                .And
                                .Contain
                                    (
                                        s => s.MappingType == MappingDefinitionType.Fluent
                                                && s.Name == "MyFluentMetadataSource"
                                                && s.Path == "LINQPad.OpenAccess.Tests.TestModel.Fluent"
                                                && s.Source is FluentMetadataSource
                                    );
        }
        [TestMethod]
        public void ReturnsEmptyCollectionIfNoSourcesAreFound( )
        {
            var metadataSources = this._locator.GetFromAssembly( typeof( IQueryable ).Assembly );

            metadataSources.Should( )
                           .NotBeNull( )
                           .And
                           .BeEmpty( );
        }
        [TestMethod]
        public void ThrowsArgumentNullExceptionIfAssemblyIsNull( )
        {
            this._locator
                .Invoking( e => e.GetFromAssembly( null ) )
                .ShouldThrow<ArgumentNullException>( );
        }
    }
}